//
//  ImageViewController.swift
//  BBAnimatedTransitioningDemo
//
//  Created by YouMao on 2022/4/28.
//

import UIKit
import BBAnimatedTransitioning

class ImageViewController: UIViewController {

    let imageView = UIImageView()
    
    let scrollView = UIScrollView()
        
    var image: UIImage =  UIImage.init(named: "th")!

    var animator: BBTransitiontingAnimator
    
    private var originalFrame: CGRect
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        self.createUI()
    }
    
    public init() {
        self.animator = BBTransitiontingAnimator()
        self.originalFrame = imageView.frame
        super.init(nibName: nil, bundle: nil)
        self.transitioningDelegate = animator
        self.animator.outAnimation = BBOutAnimation.view(imageView, to: CGRect(x: 40, y: 100, width: 192, height: 108))
        self.animator.intoAnimation = BBIntoAnimation.view(imageView, from: CGRect(x: 40, y: 100, width: 192, height: 108))
        self.modalPresentationStyle = .custom
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ImageViewController: UIScrollViewDelegate {
    
    public func scrollViewDidZoom(_ scrollView: UIScrollView) {
        var frame = imageView.frame
        let differenceHeight = scrollView.frame.size.height - imageView.frame.size.height
        let differenceWidth = scrollView.frame.size.width - imageView.frame.size.width
        frame.origin.y = differenceHeight > 0 ? differenceHeight / 2 : 0
        frame.origin.x = differenceWidth > 0 ? differenceWidth / 2 : 0
        imageView.frame = frame
        scrollView.contentSize = frame.size
    }
    
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

// MARK: - UI
extension ImageViewController {
    
    @objc func closeButtonAction() {
        self.dismiss(animated: true) {
            
        }
    }
    
    @objc func doubleTapAction(tap: UITapGestureRecognizer) {
        if scrollView.zoomScale > 1 {
            scrollView.zoomScale = 1
        } else {
            scrollView.zoomScale = 3
        }
    }
    
    func createUI() -> Void {
        view.backgroundColor = UIColor.black
        
        let tap = UITapGestureRecognizer()
        tap.numberOfTapsRequired = 1
        tap.addTarget(self, action: #selector(closeButtonAction))
        self.scrollView.addGestureRecognizer(tap)
        self.scrollView.frame = view.bounds
        self.scrollView.delegate = self
        self.scrollView.contentSize = view.bounds.size
        self.scrollView.showsVerticalScrollIndicator = false
        self.scrollView.showsHorizontalScrollIndicator = false
        self.scrollView.minimumZoomScale = 1.0
        self.scrollView.maximumZoomScale = 3.0
        view.addSubview(self.scrollView)
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction(_:)))
        view.addGestureRecognizer(pan)
        
        self.imageView.image = image
        let imageSize = image.size
        imageView.frame = CGRect(x: 0, y: (scrollView.frame.size.width - imageSize.height) / 2, width: scrollView.frame.size.width, height: scrollView.frame.size.width / imageSize.width * imageSize.height)
        imageView.center = scrollView.center
        imageView.contentMode = .scaleAspectFit
        scrollView.addSubview(imageView)
        
    }
}

// MARK: Methods
extension ImageViewController {
    
    @objc func panAction(_ gesture: UIPanGestureRecognizer) {
        if gesture.state == .began {
            originalFrame = imageView.frame
        } else if gesture.state == .changed {
            let offset = gesture.translation(in: scrollView)
            imageView.frame = CGRect(x: originalFrame.origin.x + offset.x, y: originalFrame.origin.y + offset.y, width: originalFrame.size.width, height: originalFrame.size.height)
            view.backgroundColor = .black.withAlphaComponent((originalFrame.maxY - offset.y) / (view.frame.height - originalFrame.maxY))

        } else if gesture.state == .cancelled {
            UIView.animate(withDuration: 0.3, delay: 0, options: []) {
                self.imageView.frame = self.originalFrame
                self.view.backgroundColor = .black
            } completion: { finished in
                
            }
        } else if gesture.state == .ended {
            if imageView.frame.origin.y - originalFrame.origin.y > 200 {
                scrollView.zoomScale = 1.0
                self.dismiss(animated: true)
            } else {
                UIView.animate(withDuration: 0.3, delay: 0, options: []) {
                    self.imageView.frame = self.originalFrame
                    self.view.backgroundColor = .black
                } completion: { finished in
                    
                }
            }
            
        }
    }
}
